from main import DIR, ENVIRON
import yaml
import os
from pathlib import Path


class YamlRead:
    @staticmethod
    def env_config():
        """环境变量的读取方式"""
        with open(file=f'{DIR}/data/envConfig/{ENVIRON}/config.yml', mode='r', encoding='utf-8') as f:
            return yaml.load(f, Loader=yaml.FullLoader)

    @staticmethod
    def env_config1():
        """环境变量的读取方式"""
        with open(file=f'{DIR}/data/env_config/{ENVIRON}/config.yml', mode='r', encoding='utf-8') as f:
            return yaml.load(f, Loader=yaml.FullLoader)

    @staticmethod
    def env_config2(self, env=None):
        if env is None:
            env = os.getenv("TEST_ENV", "Offline")  # 默认使用线下环境

        config_path = Path(__file__).resolve().parents[1] / f"data/env_config/{env}/config.yml"

        if not config_path.exists():
            raise FileNotFoundError(f"❌ 配置文件不存在: {config_path}")

        with open(config_path, "r", encoding="utf-8") as f:
            config = yaml.safe_load(f)

        return config

    @staticmethod
    def api_config():
        with open(file=f'{DIR}/data/apiConfig/config.yml', mode='r', encoding='utf-8') as f:
            return yaml.load(f, Loader=yaml.FullLoader)
